/*
 * client_stat.cpp
 *
 *  Created on: Jan 11, 2014
 *      Author: killerg
 */




#include "server_deps.h"
#include "client_stat.h"
#include "server_cfg.h"

#include "game_logic/proto_mgr.h"

#include "server.h"
#include "share/db_mgr.h"
#include "share/util.h"
#include "share/json/json.h"

void client_stat_t::add_consume_byid(EConsumeType ect, int32_t misc, int32_t consumeid) {
    if (consumeid < 0)
        return;

    const auto* consume_proto = MAProto(Consume, consumeid);

    consume_t co;
    co.ect = ect;
    co.misc = misc;
    if (NULL != consume_proto) {
        co.consume_money = consume_proto->money;
        co.consume_gamemoney = consume_proto->game_money;
    }
    consume_log.push_back(co);
}

void client_stat_t::add_consume_byval(EConsumeType ect, int32_t misc, int32_t money, int32_t gamemoney) {
    consume_t co;
    co.ect = ect;
    co.misc = misc;
    co.consume_money = money;
    co.consume_gamemoney = gamemoney;
    consume_log.push_back(co);
}

void client_stat_t::add_event(EEventType eet, int32_t misc) {
    event_t ev;
    ev.eet = eet;
    ev.misc = misc;
    event_log.push_back(ev);
}

void client_stat_t::reset() {
    playerid = -1;
    name = "";
    account = "";

    logon = -1;
    logoff = -1;
    conn_start = -1;
    conn_end = -1;
    create_start = -1;    // 创建玩家
    create_end = -1;
    load_start = -1;      // 加载时间
    load_end = -1;

    event_log.clear();
    consume_log.clear();
}

void client_stat_t::post_save() {
	if (!g_dbl->ok()) {
		return;
	}

    if (logoff < 0)
        return;

    Json::Value value;
    value["objtype"] = "sess";

    value["player"] = name;
    value["account"] = account;
    value["playerid"] = Json::Int64(playerid);
    value["mc"] = machine_code;
    value["logon"] = Json::Int64(logon);
    value["logoff"] = Json::Int64(logoff);

    if (conn_start >= 0 && conn_end >= 0)
        value["conn_sec"] = Json::Int64(conn_end - conn_start);
    else
        value["conn_sec"] = -1;

    if (create_start >= 0 && create_end >= 0)
        value["create_sec"] = Json::Int64(create_end - create_start);
    else
        value["create_sec"] = -1;

    if (load_start >= 0 && load_end >= 0)
        value["load_sec"] = Json::Int64(load_end - load_start);
    else
        value["load_sec"] = -1;

    Json::Value consume;
    Json::Value &consumes = value["cos"];
    for (const auto &itr : consume_log) {
        consume["t"] = itr.ect;
        consume["m"] = itr.misc;
        consume["cm"] = itr.consume_money;
        consume["cg"] = itr.consume_gamemoney;
        consumes.append(consume);
    }

    Json::Value event;
    Json::Value &events = value["evs"];
    for (const auto &itr : event_log) {
        event["t"] = itr.eet;
        event["m"] = itr.misc;
        events.append(event);
    }

    // account serverid  logontime
    string key = fmt_cstr("%s:%d:%lld", account.c_str(), g_cfg->ServerId, logon);
    g_dbl->set(key.c_str(), value, [key](db_error_t err){
        if (DBE_Success != err)
            UTIL_LOG(E, "err save stat_log %S", key.c_str());
    });
}
